import {
    BlockComponentTypes,
    GameMode,
    ItemLockMode,
    ItemStack,
    world,
} from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { BlockInteractionBlocker } from "@sapi-game/gameComponent/player/blockInteractionBlocker";
import { PlayerGroupSet } from "@sapi-game/gamePlayer/groupSet";
import { CameraPreset, EffectIds } from "@sapi-game/utils/vanila-data";
import { CvSChestManager } from "./components/chest";
import { CvSDaemon } from "./components/daemon";
import { CvSIronManager } from "./components/iron";
import { CvSLoader } from "./components/loader";
import { CvSRespawn } from "./components/respawn";
import { CvSEndState } from "./endState";
import { ChickenVsShepherdModule } from "./module";

export class CvSMainState extends ChickenVsShepherdModule.State {
    readonly chickenTag = "CvS_chicken";
    readonly ironSword = new ItemStack("iron_sword");

    override onEnter(): void {
        this.ironSword.lockMode = ItemLockMode.inventory;
        this.ironSword.nameTag = "牧人的剑";
        this.killEntites();
        this.initPlayer();
        this.addComponent(CvSLoader);
    }

    initPlayer() {
        world.gameRules.pvp = true;
        this.context.groupSet.forEach((p) => {
            p.player.setGameMode(GameMode.Adventure);
            p.clear();
            p.player.camera.clear();
            p.player.addEffect(EffectIds.Invisibility, 150, {
                showParticles: false,
            });
            //给效果
            p.player.addEffect(EffectIds.Resistance, 100, {
                amplifier: 100,
                showParticles: false,
            });
            //传到等待处
            p.player.teleport(this.context.map.watingPos);
            p.player.camera.setCamera(
                CameraPreset.Free,
                this.context.map.watingCamera
            );
        });
    }

    start() {
        this.deleteComponent(CvSLoader);
        this.addComponent(CvSDaemon)
            .addComponent(Timer, {
                initialTime: this.context.config.chestRefreshInterval,
            })
            .addComponent(CvSIronManager)
            .addComponent(CvSChestManager)
            .addComponent(CvSRespawn)
            .addComponent(BlockInteractionBlocker, {
                groupSet: new PlayerGroupSet([this.context.shepherd]),
                blockComponentType: BlockComponentTypes.Inventory,
                message: "你不能打开小鸡的箱子",
            });
        this.playersStart();
        this.getComponent(Timer).start();
    }

    /**游戏开始 */
    playersStart() {
        this.context.groupSet.forEach((p) => {
            p.player.camera.clear();
        });
        //猎人开始
        this.context.shepherd.forEach((p) => {
            p.giveItem(this.ironSword);
            p.player.teleport(this.context.map.ShepherdSpawnpoint, {
                rotation: { x: 0, y: 180 },
            });
        });
        //鸡tp
        this.context.chicken.forEach((p) => {
            const chicken = this.context.playerChickens.find(
                (e) => e.player == p
            );
            if (chicken && chicken.entity) {
                p.player.teleport(chicken.entity.location);
            }
        });
    }

    end(winner: "chicken" | "shepherd") {
        if (this.context.winner != null) return;
        this.context.winner = winner;
        this.transitionTo(CvSEndState);
    }

    killEntites() {
        const chickens = Dimensions.Overworld.getEntities({
            type: "minecraft:chicken",
            tags: [this.chickenTag],
        });
        const entites = this.context.map.region.getEntitesInRegion({
            type: "minecraft:item",
        });
        entites.forEach((e) => e.remove());
        chickens.forEach((e) => e.remove());
    }

    override onExit(): void {
        this.killEntites();
    }
}
